package com.zhiyou100.www.bbs.login;

import com.zhiyou100.www.bbs.dao.TableUserDao;
import com.zhiyou100.www.bbs.dao.UserLoginFormDao;
import com.zhiyou100.www.bbs.entityclass.TableUser;

import javax.jms.Session;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @packageName: ${NAME}
 * @className: ${PACKAGE_NAME}
 * @Description: TODO 登陆页面
 * @author: yang
 * @date: 2020/6/4
 */
@WebServlet(name = "Login", urlPatterns = "/login")
public class Login extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 设定类型


        String userName = request.getParameter("userName");
        String userPwd = request.getParameter("userPwd");
        // 获取参数


        TableUserDao tableUserDao = new TableUserDao();
        if (tableUserDao.login(userName, userPwd)) {
            // 如果登陆成功
            HttpSession servletContext = request.getSession();
            // 获取Session对象
            servletContext.setAttribute("user", new TableUser(userName, userPwd));
            // 把登陆密码放入
            Object loginNumber = servletContext.getAttribute("loginNumber");
            // 获取当前的登陆数量
            if (loginNumber == null) {
                // 为null说明当前登陆者是第一个
                servletContext.setAttribute("loginNumber", 1);
            } else {
                // 如果不是第一个，值+1
                servletContext.setAttribute("loginNumber", (Integer) (loginNumber) + 1);
            }
            response.sendRedirect("/bbs/jsp/homepage.jsp");
            // 转发到homepage.jsp
        } else {
            // 登陆失败
            System.out.println("登陆失败！！！！！！！！");
            response.sendRedirect("/bbs/html/loginFailure.html");
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 设定类型

        PrintWriter writer = response.getWriter();
        String web = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>登陆</title>\n" +
                "    <link rel=\"stylesheet\" href=\"/bbs/css/login.css\">\n" +
                "    <!--  链接样式表   -->\n" +
                "    <script src=\"/bbs/js/jquery.min.js\"></script>\n" +
                "    <!-- 导入 jquery    -->\n" +
                "    <script src=\"bbs/js/login.js\"></script>\n" +
                "    <!--  判断是否为空  -->\n" +
                "\n" +
                "</head>\n" +
                "<body>\n" +
                "<h1 align=\"center\">登陆</h1>\n" +
                "<form action=\"/bbs/login\" method=\"post\">\n" +
                "    <table>\n" +
                "        <tr>\n" +
                "            <th>用户名:</th>\n" +
                "            <td><label>\n" +
                "                <input type=\"text\" name=\"userName\">\n" +
                "            </label></td>\n" +
                "        </tr>\n" +
                "        <tr>\n" +
                "            <th>密 码：</th>\n" +
                "            <td>\n" +
                "                <label ><input type=\"password\" name=\"userPwd\"></label>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "        <tr>\n" +
                "            <th colspan=\"2\">\n" +
                "                <input type=\"reset\" value=\"重新填写\">\n" +
                "                <input type=\"submit\" value=\"登陆\">\n" +
                "            </th>\n" +
                "        </tr>\n" +
                "    </table>\n" +
                "</form>\n" +
                "\n" +
                "\n" +
                "</body>\n" +
                "</html>";
        writer.write(web);
        writer.flush();
    }

    @Override
    public void destroy() {
        super.destroy();
        // 当 svelte 关闭时，清空 用户登陆表
        new UserLoginFormDao().dropTheTable();
        // 清空
    }
}
